<template>
	<view class="search-box">
		<view class="input-box">
			<input class="search-iput" type="text" v-model="searchValue" placeholder="请输入搜索内容" />
			<view class="close-box" v-if="searchValue.length>0" @tap="clearVal">
				<u-icon name="close" color="#777" size="28"></u-icon>
			</view>
		</view>
		<view class="search-icon">
			<u-icon name="search" color="#ffffff" size="40" @tap="tapSearch"></u-icon>
		</view>
	</view>
</template>

<script>
	import {setHistoryStorage} from "@/utils/setStorage.js"
	export default {
		name:"searchTop",
		props:{
			
		},
		methods:{
			/*  点击搜索  */
			tapSearch(){
				let val = this.$store.state.search.searchValue
				val = val.trim()
				if(val.length>0){
					setHistoryStorage(val)
				}
				this.$emit("toSearch")
			},
			
			/*  清除值  */
			clearVal(){
				this.$store.commit("search/setSearchValue","")
			}
		},
		
		computed:{
			searchValue:{
				get(){
					return this.$store.state.search.searchValue
				},
				set(v){
					this.$store.commit("search/setSearchValue",v)
				}
			},
		}
	}
</script>

<style scoped lang="scss">
	.search-box{
		width: 100%;
		display: flex;
		justify-content: space-between;
		align-items: center;
		.input-box{
			flex-grow: 1;
			position: relative;
		}
		.close-box{
			position: absolute;
			right: 0;
			top: 0;
			height: 60rpx;
			line-height: 60rpx;
			width: 80rpx;
			text-align: center;
			border-radius: 60rpx 60rpx;
		}
		.search-iput{
			width: 100%;
			flex-grow: 1;
			color: #666666;
			font-size: 28rpx;
			height: 60rpx;
			border-radius: 60rpx 60rpx;
			background: #FFFFFF;
			padding-left: 20rpx;
			padding-right: 20rpx;
		}
		.search-icon{
			flex-grow: 0;
			padding-left: 20rpx;
		}
	}
</style>
